function toLogin(){ 
    var iWidth = 750;
    var iHeight = 500;
    var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
    var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
	window.open("http://888pic.com/?m=login&a=snsLogin&type=qq","","width="+iWidth+", height="+iHeight+", top="+iTop+", left="+iLeft); 
}

function showLogin(t, n){
	$(".Login").find('.login-title').text(t);
	$(".Login").find('.login-notice').text(n);
    var iconQQ = $(".Login").find('.icon-qq');
    if(!iconQQ.attr('src')){
        iconQQ.attr('src', iconQQ.attr('data-original'));
    }
	$(".Login").fadeIn(300);
}

function cookie(name) {
    var cookieArray = document.cookie.split("; ");
    var cookie = new Object();
    for (var i = 0; i < cookieArray.length; i++) {
        var arr = cookieArray[i].split("=");
        if (arr[0] == name) return unescape(arr[1]);
    }
    return "";
}

function search(kw){
    $.get("http://888pic.com/?m=search&a=check", {kw:kw}, function(data) {
        if(data.status == 1){
            tj_keyword()
            setTimeout(function(){
                window.location.href="http://888pic.com/tupian/"+data.py+".html";
            },500);
        }else{
            alert('请填写有效的关键词');
        }
    }, 'json');
}

$(function(){
	//个人信息的显示与隐藏
	$(".user-infor").hover(function(){
        if(!$('#userhead').attr('src')){
            $('#userhead').attr('src', $('#userhead').attr('data-original'));
        }
		$(this).find('.user-info').fadeIn(200);
	},function(){
		$(this).find('.user-info').fadeOut(200);
	});

   //注册登录弹窗的显示和隐藏
	$(".user-top .register").click(function(){
		showLogin('注册包图网', 'QQ一键注册');
	});
	$(".Login .close-but").click(function(){
		$(".Login").fadeOut(300);
	});

	// 返回顶部
    $("body").on("click","#gotop",function(e) {
        $('body,html').animate({scrollTop:0},300);
    });
    $(window).scroll(function(e) {
        if($(window).scrollTop()>200)
            $("#gotop").show();
        else
            $("#gotop").hide();
    });

    var formSearch = function(){
        var kw = $.trim($('#so-kw').val());
        if(kw == ''){
            alert('请填写关键词');
            return false;
        }
        search(kw);
    }
    $('.but-search').on('click', formSearch);
    $('#so-kw').keydown(function(event){if(event.which == 13){formSearch();}});

    // 收藏按钮 - 适用于首页、分类页、搜索页和“我看过的”
    $('.browse,.content-index').on('click', '.collect', function(){
        if(cookie('auth_id') == ""){
            showLogin('登录包图网', '登录后可收藏');
            return false;
        }
        var $this = $(this);
        var id = parseInt($this.attr('data-id'));
        var status = parseInt($this.attr('data-status'));
        var data = {id:id,status:status};
        $.ajax({
            url: 'http://888pic.com/?m=favourite',
            data: data,
            dataType: 'json',
            success:function(r){
                if(r.status == 1){
                    if(status == 0){
                        $this.attr('data-status', 1).html('<i></i>已收藏');
                    }else if(status == 1){
                        $this.attr('data-status', 0).html('<i></i>收藏');
                    }
                }
            }
        })
    });

    $("img.lazy").lazyload({effect: "fadeIn"});

    // 反馈弹窗的关闭计开启
    $(function(){
        $(".fankui").click(function(){
            $(".feedback").fadeIn(300);
        })
        $(".feedback").on('click', '.sub-but-no', function(){
            $(".feedback").fadeOut(300);
        })
        $(".feedback").on('click', '.sub-but', function(){
                var advise = $.trim($('textarea[name=advise-ctx]').val());
                var qq = parseInt($(':input[name=adviser-qq]').val());
                var data = {advise:advise,qq:qq};
                $.ajax({
                    type:'post',
                    url:'http://888pic.com/?m=advise',
                    data:data,
                    dataType:'json',
                    success:function(rs){
                        if(rs.status == 1){
                            $(".feedback-box").fadeOut(300).siblings().show();
                            $(".feedback").delay(2000).fadeOut(300);
                        }else{
                            alert(rs.msg);
                        }
                    }
                });
            });
    });

    $(".discount-vip").mousedown(function() {
        $.getJSON("http://ajax.888pic.com/?m=clickstats&lx=3&callback=?", function(e) {});
    });
});

$(function(){
    var loaded = false;
    var a=true;
    $(".fixed-but .my-browse-but").click(function(event){
        event.stopPropagation();
        var auth_id = cookie('auth_id');
        if(auth_id == ""){
            showLogin('登录包图网', '登录后可查看浏览记录');
            return false;
        }
        if(!loaded){
            $('.browse-center').html('');
            $('.my-browse span').text('');
            $.get("http://888pic.com/?m=footprints", function(data) {
                // console.log(data)
                if(data.status == 1){
                    $('.my-browse span').text(data.extra.count);
                    for(var i in data.data){
                        var body = '<div class="browse-center-list"><div class="browse-center-date"><span class="icon"></span><i class="date">'+data.data[i]['date']+'</i></div><div class="img-show-box clearfix">';
                        var list = data.data[i]['list'];
                        for(var l in list){
                            body += '<div class="list fl"><div class="img"><img src="'+data.extra.pic_domain+list[l]['picurl']+data.extra.pic_suffix+'" width="100" height="152"></div><div class="img-pos"><div class="img-pos-bg"></div><div class="img-pos-but"><a class="collect" data-id="'+list[l]['id']+'" data-status="0"><i></i>收藏</a></div><a href="http://888pic.com/'+list[l]['url']+'" target="_blank" class="img-pos-a"><div class="img-pos-a-but"></div></a></div></div>';
                        }

                        body += '</div></div>';
                        $('.browse-center').append(body);
                    }
                    $('.browse-center').append('<div class="more-views"><a href="http://888pic.com/?m=home&a=myFootprints" target="_blank">查看更多记录</a></div>');
                    $("img.lazy").lazyload({container:$(".browse")});
                    loaded = true;
                    setTimeout(function(){loaded = false}, 10000);
                }else if(data.status == 2){
                    $('.my-browse span').text('0');
                    $('.browse-center').html('<div style="padding:25px;text-align:center">'+data.msg+'</div>');
                }
            }, 'json');
        }
        if(a){
            $('#browse').animate({right:'0'},"fast");
            $(".fixed-but").animate({right:'250px'},"fast");
                a=false;
                $('.my-browse-but').addClass('my-browse-but-on');
        }else{
            $('#browse').animate({right:'-300px'},"fast");
            $(".fixed-but").animate({right:'0'},"fast");
            a =true;
            $('.my-browse-but').removeClass('my-browse-but-on');
       }
    });

    $(".close-but").click(function(event){
        event.stopPropagation();
        $('#browse').animate({right:'-300px'},"fast");
        $(".fixed-but").animate({right:'0'},"fast");
        a =true;
        $('.my-browse-but').removeClass('my-browse-but-on');
    });

    //点击页面其它地方隐藏我浏览过的
    $('#main').click(function(event){
        event.stopPropagation();
        $('body').find("#browse").animate({right:'-300px'},"fast");
        $(".fixed-but").animate({right:'0'},"fast");
        a =true;
        $('.my-browse-but').removeClass('my-browse-but-on');
    });

    $("#browse").hover(function(event){
        var top = $(document).scrollTop();
        $(document).scroll(function(){
            $(window).scrollTop(top);
        });

    },function(){
        $(document).unbind('scroll');
    });

});

//网站公共头部个人头像下拉框点击充值会员点击
$(".pay-member").mousedown(function() {
    $.getJSON("http://ajax.888pic.com/?m=clickstats&lx=3&callback=?", function(e) {});
});

!function(a,b,c,d){var e=a(b);a.fn.lazyload=function(f){function g(){var b=0;i.each(function(){var c=a(this);if(!j.skip_invisible||c.is(":visible"))if(a.abovethetop(this,j)||a.leftofbegin(this,j));else if(a.belowthefold(this,j)||a.rightoffold(this,j)){if(++b>j.failure_limit)return!1}else c.trigger("appear"),b=0})}var h,i=this,j={threshold:0,failure_limit:0,event:"scroll",effect:"show",container:b,data_attribute:"original",skip_invisible:!0,appear:null,load:null,placeholder:""};return f&&(d!==f.failurelimit&&(f.failure_limit=f.failurelimit,delete f.failurelimit),d!==f.effectspeed&&(f.effect_speed=f.effectspeed,delete f.effectspeed),a.extend(j,f)),h=j.container===d||j.container===b?e:a(j.container),0===j.event.indexOf("scroll")&&h.bind(j.event,function(){return g()}),this.each(function(){var b=this,c=a(b);b.loaded=!1,(c.attr("src")===d||c.attr("src")===!1)&&c.is("img")&&c.attr("src",j.placeholder),c.one("appear",function(){if(!this.loaded){if(j.appear){var d=i.length;j.appear.call(b,d,j)}a("<img />").bind("load",function(){var d=c.attr("data-"+j.data_attribute);c.hide(),c.is("img")?c.attr("src",d):c.css("background-image","url('"+d+"')"),c[j.effect](j.effect_speed),b.loaded=!0;var e=a.grep(i,function(a){return!a.loaded});if(i=a(e),j.load){var f=i.length;j.load.call(b,f,j)}}).attr("src",c.attr("data-"+j.data_attribute))}}),0!==j.event.indexOf("scroll")&&c.bind(j.event,function(){b.loaded||c.trigger("appear")})}),e.bind("resize",function(){g()}),/(?:iphone|ipod|ipad).*os 5/gi.test(navigator.appVersion)&&e.bind("pageshow",function(b){b.originalEvent&&b.originalEvent.persisted&&i.each(function(){a(this).trigger("appear")})}),a(c).ready(function(){g()}),this},a.belowthefold=function(c,f){var g;return g=f.container===d||f.container===b?(b.innerHeight?b.innerHeight:e.height())+e.scrollTop():a(f.container).offset().top+a(f.container).height(),g<=a(c).offset().top-f.threshold},a.rightoffold=function(c,f){var g;return g=f.container===d||f.container===b?e.width()+e.scrollLeft():a(f.container).offset().left+a(f.container).width(),g<=a(c).offset().left-f.threshold},a.abovethetop=function(c,f){var g;return g=f.container===d||f.container===b?e.scrollTop():a(f.container).offset().top,g>=a(c).offset().top+f.threshold+a(c).height()},a.leftofbegin=function(c,f){var g;return g=f.container===d||f.container===b?e.scrollLeft():a(f.container).offset().left,g>=a(c).offset().left+f.threshold+a(c).width()},a.inviewport=function(b,c){return!(a.rightoffold(b,c)||a.leftofbegin(b,c)||a.belowthefold(b,c)||a.abovethetop(b,c))},a.extend(a.expr[":"],{"below-the-fold":function(b){return a.belowthefold(b,{threshold:0})},"above-the-top":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-screen":function(b){return a.rightoffold(b,{threshold:0})},"left-of-screen":function(b){return!a.rightoffold(b,{threshold:0})},"in-viewport":function(b){return a.inviewport(b,{threshold:0})},"above-the-fold":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-fold":function(b){return a.rightoffold(b,{threshold:0})},"left-of-fold":function(b){return!a.rightoffold(b,{threshold:0})}})}(jQuery,window,document);
//搜索
$(function() {
    //默认搜索词
    $.ajax({
        type: 'POST',
        url: '/index.php?m=Ajaxsearch&a=def',
        data: {id: 1},
        dataType: 'json',
        success: function (rs) {
            $('#history').html('');
            $('.z_qufenxian').html('');
            var html = '';
            var html2 = '';
            var num = 1;
            for (var i in rs.new) {
                if(rs.new[i] != null){
                    html = '<p id="u_'+num+'" onclick="on_search('+ "'"+ rs.new[i].name + "'" +',this)">' +
                        '<span class="z_spanName">' + rs.new[i].name + '</span>' +
                        '<span class="z_spanSearch">' + rs.new[i].count + '结果</span>' +
                        '</p>'
                    $('#history').append(html);
                    num++;
                }
            }
            var l = 1;
            for (var j in rs.newTop) {
                if (l <= 3) {
                    html2 = '<p id="u_'+num+'" onclick="on_search('+ "'"+ rs.newTop[j].name + "'" +',this)">' +
                        '<span class="z_box z_box' + l + '">' + l + '</span>' +
                        '<span class="z_spanName">' + rs.newTop[j].name + '</span>' +
                        '<span class="z_spanSearch">' + rs.newTop[j].count + '结果</span>' +
                        '</p>';
                } else {
                    html2 = '<p id="u_'+num+'" onclick="on_search('+ "'"+ rs.newTop[j].name + "'" +',this)">' +
                        '<span class="z_box">' + l + '</span>' +
                        '<span class="z_spanName">' + rs.newTop[j].name + '</span>' +
                        '<span class="z_spanSearch">' + rs.newTop[j].count + '结果</span>' +
                        '</p>';
                }
                num++;
                l++
                $('#top').append(html2);
            }
        }
    });
})
//搜索
$("#so-kw").keyup(function(H){
    if (window.event) {
        var G = window.event.keyCode;
    } else {
        var G = H.which;
    }
    if (G != 38 && G != 40 && G != 13) {
        var num = 1;
        D = 0;
        if($('#so-kw').val() == ''){
            //默认搜索词
        }else{
            $('#sort').remove()
            $.ajax({
                type: 'POST',
                url: '/index.php?m=Ajaxsearch&a=index',
                data: {keyword: $('#so-kw').val()},
                dataType: 'json',
                success: function (rs) {
                    $('#history').html('');
                    $('#top').html('');
                    var html = '';
                    for (var i in rs) {
                        html = '<p id="u_'+num+'" onclick="on_search('+ "'"+rs[i].keyword + "'" +',this)">' +
                            '<span class="z_spanName">' + rs[i].keyword + '</span>' +
                            '<span class="z_spanSearch">' + rs[i].icount + '结果</span>' +
                            '</p>'
                        $('#history').append(html);
                        num++
                    }
                }
            });
        }
    } else {
        topkey(H)
    }
});
//下拉点击搜索
function on_search(keyword,e){
    $('#so-kw').val(keyword)
    var sort =  $(e).attr('id')
    $('#sort').val(sort)
    search(keyword)

}
(function () {
    var z_so = document.getElementById('so-kw') ;
    var z_xiala = document.getElementById('z_xiala') ;
    z_so.addEventListener('focus',function () {
        z_xiala.style.display = 'block';
    }) ;
    z_so.addEventListener('blur',function () {
        setTimeout(function(){
            //记录完成再隐藏
            z_xiala.style.display = 'none' ;
        },200);

    });
})()


var D = 0;
function topkey(H) {
    if (window.event) {
        var G = window.event.keyCode;
    } else {
        var G = H.which;
    }
    if (G != 38 && G != 40 && G != 13) {
        D = 0;
    } else {
        if ($("#z_xiala").css("display") == "block") {
            var I = $(".z_spanName").length;
            if (G == 38) {
                D--;
                if (D < 1) {
                    D = I;
                    C(D);
                    F(1);
                } else {
                    F(D + 1);
                    C(D);
                }
                return false;
            }
            if (G == 40) {
                D++;
                if (D > I) {
                    D = 1;
                    C(1);
                    F(I);
                } else {
                    F(D - 1);
                    C(D);
                }
                return false;
            }
        } else {
            D = 0;
        }
    }
}

function C(G) {
    $("#u_" + G).css("background-color", "#EBEBEB");
    var name = $("#u_" + G + " .z_spanName").html()
    $('#sort').val("#u_" + G)
    $('#so-kw').val(name)
}
function F(G) {
    $("#u_" + G).css("background-color", "#fff");
}
//统计点击位置
function tj_keyword(){
    var sort = $('#sort').val()
    $.ajax({
        type: 'POST',
        url: 'http://ajax.888pic.com/?m=search&a=index',
        data: {sort:sort},
        dataType: 'json',
        success: function (rs) {
        }
    });
}
